<template>
  <div id="Index">
    <router-view></router-view>
  </div>
</template>

<script lang="ts">
import {Component, Vue, Watch} from 'vue-property-decorator'
import util from '@/util/util'

@Component
export default class Index extends Vue {
  // 获取code
  getToCode() {
    let href = process.env.VUE_APP_UAT + encodeURIComponent(location.href) + '&response_type=code&state=123'
    location.href = href
  }

  // 登录
  goToLogin() {
    let href = process.env.VUE_APP_LOGIN
    location.href = href
  }

  // 通过code获取用户信息
  getUserInfo(code: any) {
    let jumpUrl = process.env.VUE_APP_HREF;
    this.$api.memberMemberInfoFetch({code}).then((res: any) => {
      console.log(res, 'res')
      if (res.code == 0) {
        localStorage.setItem('userInfo', JSON.stringify(res.data))
        localStorage.setItem('sessionId', res.data.sessionId)
        let path = localStorage.getItem('path') || '/systemManage/userInfo';
        window.location.replace(`${jumpUrl}${path}`)
      }
    }).catch((err: any) => {
      if (err == '没有权限') {
        location.href = process.env.VUE_APP_UAT_NO_PERMISSION
      }
    });
  }

  mounted() {


    // let jumpUrl = process.env.VUE_APP_ENV == 'dev' ? 'http://localhost:8081/#' : (process.env.VUE_APP_ENV == 'test' ? 'http://192.168.50.134:9021/#' : 'https://gth5.jclang.me:8666/#')
    if (this.$route.path == '/' || this.$route.path == '/login') {
      this.goToLogin()
    } else {
      if (this.$route.query.code) {

        this.getUserInfo(this.$route.query.code)

      } else {
        this.getToCode()
      }
    }

  }
}
</script>

<style lang="scss">

</style>
